package com.assignwork.service.impl;

import com.assignwork.dto.DailyShiftSearchDTO;
import com.assignwork.dto.DailyShiftSubmitDTO;
import com.assignwork.entity.DailyShift;
import com.assignwork.mapper.DailyShiftMapper;
import com.assignwork.service.IDailyShiftService;
import com.assignwork.vo.DailyShiftVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DailyShiftServiceImpl
 * @Description 每日作业班组接口实现
 * @Author dujianyu
 * @Date 2023/12/1 10:51
 */
@Service
public class DailyShiftServiceImpl extends BaseServiceImpl<DailyShiftMapper, DailyShift> implements IDailyShiftService {

    HashMap<Integer, String> shifts = new HashMap<Integer, String>(){{
        put(0,"D");
        put(1,"A");
        put(2,"C");
        put(3,"D");
        put(4,"B");
        put(5,"C");
        put(6,"A");
        put(7,"B");
    }};

    @Override
    public boolean generateDailyShift() {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate startDate = LocalDate.parse("2023-01-01",fmt);
        LocalDate endDate = LocalDate.parse("2023-12-31",fmt);
        int loopTime = Func.toInt(startDate.until(endDate, ChronoUnit.DAYS));

        LocalDate theDay = startDate;
        for (int i = 0; i < loopTime; i++) {
            int sub = Func.toInt(startDate.until(theDay, ChronoUnit.DAYS));
            int code1 = (sub * 2) % 8;
            String dailyShift1 = shifts.get(code1);

            DailyShift ds1 = new DailyShift();
            ds1.setWorkDate(theDay);
            ds1.setTimeSpace("DAY");
            ds1.setDailyShift(dailyShift1);
            super.saveOrUpdate(ds1);

            int code2 = (sub * 2) % 8 + 1;
            String dailyShift2 = shifts.get(code2);
            DailyShift ds2 = new DailyShift();
            ds2.setWorkDate(theDay);
            ds2.setTimeSpace("NIGHT");
            ds2.setDailyShift(dailyShift2);
            super.saveOrUpdate(ds2);

            theDay = theDay.plusDays(1);
        }
        return true;
    }

    @Override
    public IPage<DailyShiftVO> selectDailyShiftPage(DailyShiftSearchDTO dto, IPage<DailyShiftVO> page) {
        List<DailyShiftVO> data = baseMapper.selectDailyShiftList(dto, page);
        for (DailyShiftVO vo : data) {
            vo.setDailyShiftName(DictBizCache.getValue("daily_shift", vo.getDailyShift()));
            vo.setTimeSpaceName(DictBizCache.getValue("time_space", vo.getTimeSpace()));
        }
        return page.setRecords(data);
    }

    @Override
    public List<DailyShiftVO> selectDailyShiftList(DailyShiftSearchDTO dto) {
        List<DailyShiftVO> data = baseMapper.selectDailyShiftList(dto, null);
        for (DailyShiftVO vo : data) {
            vo.setDailyShiftName(DictBizCache.getValue("daily_shift", vo.getDailyShift()));
            vo.setTimeSpaceName(DictBizCache.getValue("time_space", vo.getTimeSpace()));
        }
        return data;
    }

    @Override
    public boolean submitDailyShift(DailyShiftSubmitDTO dto) {
        if (Func.isNull(dto.getId())) {
            throw new RuntimeException("没有主键");
        }
        DailyShift entity = BeanUtil.copy(dto, DailyShift.class);
        return super.saveOrUpdate(entity);
    }

    @Override
    public boolean updateAssignUser(DailyShiftSubmitDTO dto) {
        QueryWrapper<DailyShift> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DailyShift::getIsDeleted, 0)
                .eq(DailyShift::getWorkDate, dto.getWorkDate())
                .eq(DailyShift::getTimeSpace, dto.getTimeSpace())
                .last("limit 1");
        DailyShift dailyShift = baseMapper.selectOne(queryWrapper);
        if (Func.isNull(dailyShift)) {
            throw new RuntimeException("没有班组，无法更新调度主任");
        }
        dailyShift.setAssignLeader(dto.getAssignLeader());
        return super.saveOrUpdate(dailyShift);
    }

}
